tempalte<typename YType>
inline __attribute__((always_inline)) void CopyUbufToGmAlignB16(unit64_t y_gm offset,__ubuf__ int32_t *src, AscendC::GlobalTensor<YType> &y&gm)
{
    AscendC::DataCopyExParams dataCopyParams(1, sizeof(int32_t), 0, 0, 0);
    AscendC::LocalTensor<YType> ubTensor;
    AscendC::TBuffAddr ubAddr;
    ubAddr.logicPos = static_cast<unint8_t>(AscendC::Tposition::VECIN);
    ubAddr.bufferAddr = reinterpret_cast<uint64_t>(src);
    ubTensor.SetAddr(ubAddr);

    copy_ub_to_gm<YType>(y_gm_offset, dataCopyParams, ubTensor, y_gm);
}

template<typename YType>
inline __attribute__((always_inline)) __aicore__ void CopyGmToUbufAlignB16(unit64_t y_gm_offset,__ubuf__ int32_t *dst,AscendC::GlobalTensor<YType> &y_gm)
{
    AscendC::DataCopyExParams dataCopyParams(1, sizeof(int32_t), 0, 0, 0);
    AscendC::LocalTensor<YType> ubTensor;
    AscendC::TBuffAddr ubAddr;
    ubAddr.logicPos = static_cast<unint8_t>(AscendC::Tposition::VECIN);
    ubAddr.bufferAddr = reinterpret_cast<uint64_t>(dst);
    ubTensor.SetAddr(ubAddr);
    
    AscendC::DataCopyPadExtParams<YType> padParams;
    DataCopypad(ubTensor, y_gm[y_gm_offset], dataCopyParams, padParams);
}

template<typename YType>
inline __attribute__((always_inline)) __aicore__ void SetBuffFlag(__ubuf__ int32_t *ctrl_flags_UB,
                                                                uint64_t y_gm_offset,
                                                                AscendC::GlobalTensor<YType> &y_gm,
                                                                int32_t flag)
{
    *ctrl_flags_UB = flag;
    AscendC::SetFlag<AscendC::HardEvent::S_MTE3>(EVENT_ID2);
    AscendC::WaitFlag<AscendC::HardEvent::S_MTE3>(EVENT_ID2);
    CopyUbufToGmAlignB16(y_gm_offset, ctrl_flags_UB, y_gm);
}

template<typename YType>
inline __attribute__((always_inline)) __aicore__ void SetBuffFlagByAdd(__ubuf__ int32_t *ctrl_flags_UB,
                                                                    uint64_t y_gm_offset,
                                                                    AscendC::GlobalTensor<YType> &y_gm,
                                                                    int32_t flag)
{
    *ctrl_flags_UB = flag;
    AscendC::SetFlag<AscendC::HardEvent::S_MTE3>(EVENT_ID2);
    AscendC::WaitFlag<AscendC::HardEvent::S_MTE3>(EVENT_ID2);
    CopyUbufToGmAlignB16(y_gm_offset, ctrl_flags_UB, y_gm);
}

template<typename YType>
inline __attribute__((always_inline)) __aicore__ void SetBuffFlag(__ubuf__ int32_t *ctrl_flags_UB,
                                                                uint64_t y_gm_offset,
                                                                AscendC::GlobalTensor<YType> &y_gm,
                                                                int32_t flag)
{
    // AscendC::PipeBarrier<PIPE_ALL>();
    *ctrl_flags_UB = flag;
    AscendC::PipeBarrier<PIPE_ALL>();
    AscendC::SetAtomicAdd<int32_t>();
    //AscendC::PipeBarrier<PIPE_ALL>();
    CopyUbufToGmAlignB16(y_gm_offset, ctrl_flags_UB, y_gm);
    AscendC::PipeBarrier<PIPE_ALL>();
    AscendC::SetAtomicNone();
    // AscendC::PipeBarrier<PIPE_ALL>();
}

template<typename YType>
inline __attribute__((always_inline)) __aicore__ void SetBuffFlag(__ubuf__ int32_t *ctrl_flags_UB, \
                                                                uint64_t y_gm_offset,
                                                                AscendC::GlobalTensor<YType> &y_gm, int32_t flag)
{
    AscendC::SetFlag<AscendC::HardEvent::MTE3_MTE2>(EVENT_ID1);
    AscendC::WaitFlag<AscendC::HardEvent::MTE3_MTE2>(EVENT_ID2);
    while(true)
    {
        CopyGmToUbufAlignB16(y_gm_offset, ctrl_flags_UB, y_gm);
        AscendC::SetFlag<AscendC::HardEvent::MTE2_S>(EVENT_ID3);
        AscendC::WaitFlag<AscendC::HardEvent::MTE2_S>(EVENT_ID3); //Scalar等MTE2
        //("ctrl_flags_UB = %d\n", *ctrl_flags_UB);
        if(*ctrl_flags_UB == flag)
        {
            break;
        }
    }
} 